#include "smallsatos.h"
#include "init.h"
#include "services.h"
#include "tasks.h"

OS_STK lancer_emission_stk[100L], lancer_experience_stk[80L],
	   lancer_mesure_dose_stk[80L], lancer_mesure_temperature_stk[80L],
	   /*lancer_mesure_puissance_stk[64L],*/ /*calculer_puissance_stk[64L],*/ ina209_driver_stk[80L],
	   eeprom_driver_stk[100L], event_log_stk[100L],
	   can_tx_stk[64L], can_rx_stk[80L],
	   incoming_stk[100L], rtc_stk[100L], service_stk[80L];

extern INT8U mode;

/**
  * Main procedure
  */
void main ()
{
	evenement_t event;
	init(); /* Initialize the microcontroller */

	OSInit();

	init_power_lock();

	/* RTC */
	OSTaskCreate(rtc_task, (void *)0, rtc_stk , 2);

	/* CAN */
	OSTaskCreate(can_rx, (void *)0, can_rx_stk , 3);
	OSTaskCreate(can_tx, (void *)0, can_tx_stk , 4);

	/* INA209 */
	OSTaskCreate(ina209_driver, (void *)0, ina209_driver_stk , 5);

	/* I2C */
	//TaskCreate(i2c_read, i2c_read_stk, 5, "I2C Read");
	//TaskCreate(i2c_write, i2c_write_stk, 6, "I2C Write");

	/* EEPROM */
	OSTaskCreate(eeprom_driver, (void *)0, eeprom_driver_stk , 6);

	/* Event log */
	OSTaskCreate(event_log, (void *)0, event_log_stk , 7);

	/* CAN msg decoder */
	OSTaskCreate(incoming, (void *)0, incoming_stk , 8);

	/* Tasks */
	switch (mode)
	{
		case PRELAUNCH:
			/* 30 min wait */
			OSTaskCreate(attente_lancement, (void *)0, service_stk , 9);
			break;

		case ANT_DEPLOYMENT:
//			OSTaskCreate(lancer_mesure_puissance, (void *)0, lancer_mesure_puissance_stk , 9);
			//OSTaskCreate(calculer_puissance, (void *)0, calculer_puissance_stk , 10);
			//OSTaskCreate(deploiement_antennes, (void *)0, service_stk , 9);
			OSTaskCreate(lancer_mesure_temperature, (void *)0, lancer_mesure_temperature_stk , 11);
			OSTaskCreate(lancer_mesure_dose, (void *)0, lancer_mesure_dose_stk , 12);	
			OSTaskCreate(lancer_experience, (void *)0, lancer_experience_stk , 13);
			OSTaskCreate(lancer_emission, (void *)0, lancer_emission_stk , 14);
			break;

		case SAFE:
			OSTaskCreate(lancer_mesure_temperature, (void *)0, lancer_mesure_temperature_stk , 11);
			OSTaskCreate(lancer_mesure_dose, (void *)0, lancer_mesure_dose_stk , 12);	
			OSTaskCreate(lancer_experience, (void *)0, lancer_experience_stk , 13);
			break;

		case MISSION:
			//TaskCreate(lancer_mesure_puissance, lancer_mesure_puissance_stk, 14, names[6]);
			OSTaskCreate(lancer_mesure_temperature, (void *)0, lancer_mesure_temperature_stk , 10);
			OSTaskCreate(lancer_mesure_dose, (void *)0, lancer_mesure_dose_stk , 11);	
			OSTaskCreate(lancer_experience, (void *)0, lancer_experience_stk , 12);
			OSTaskCreate(lancer_emission, (void *)0, lancer_emission_stk , 13);
			break;
	}	


	/* Error detection */
	//TaskCreate(error_task, error_stk, 13, names[5], 100);

	OSStart();
}
